<template>
  <div id="map">
    <baidu-map class="map"
               :mapClick="false"
               :center="center"
               ak="TdjjUm3DDvXztsh31KKjogByHtfBVfAw"
               :scroll-wheel-zoom="true"
               :zoom="zoom"
               @zoomend="syncCenterAndZoom"
               :mapStyle="mapStyle">
      <bm-map-type :map-types="['BMAP_NORMAL_MAP', 'BMAP_SATELLITE_MAP']"></bm-map-type>
      <bm-info-window :show="false"></bm-info-window>
      <div label="1111">
        <div v-for="(position, index) in positions"
             :key="index"
             class="qq"
             style="position:relative;">
          <sitIcon :position="position"
                   :zoom="zoom"
                   @tag="showSitInfo(position.code)"
                   v-show="sitIndex!=position.code && type"
                   :type="type"
                   v-if="position[type]!==null"></sitIcon>
          <sitPop :position="position"
                  v-show="sitIndex===position.code"
                  :type="type"
                  @closeP="$emit('showInfo', '')"></sitPop>
        </div>
      </div>
    </baidu-map>
  </div>
</template>

<script>
import BaiduMap from 'vue-baidu-map/components/map/Map.vue'
import sitPop from './sitPop'
import sitIcon from './sitIcon'
import { BmMapType, BmInfoWindow } from 'vue-baidu-map'
export default {
  name: 'MyMap',
  props: ['positions', 'type', 'showSit'],
  components: {
    BaiduMap,
    BmInfoWindow,
    BmMapType,
    sitPop,
    sitIcon
  },
  data () {
    return {
      zoom: 14,
      center: '扬州',
      sitIndex: null,
      huanIndex: '',
      mapStyle: {
        styleJson: [
          {
            featureType: 'highway',
            elementType: 'all',
            stylers: {
              color: '#FFFFFF'
            }
          }
        ]
      }
    }
  },
  methods: {
    // 关闭pop
    closePop () {
      this.sitIndex = null
      this.$emit('clearSit')
    },
    hideP () {
      this.$emit('hideSit')
    },
    // 显示设备详细+跳转中心+设置zoom
    showSitInfo (code) {
      this.sitIndex = code
      var data
      this.positions.forEach(item => {
        if (item.code === code) {
          data = item
        }
      })
      this.center = {
        lng: data.lng,
        lat: data.lat
      }
      this.zoom = 15
      this.$emit('showInfo', data)
    },
    // 双向绑定zoom
    syncCenterAndZoom (e) {
      this.zoom = e.target.getZoom()
    }
  },
  created () {
    this.$bus.$on('showCenter', (code) => {
      this.showSitInfo(code)
    })
  },
  watch: {
    showSit (value) {
      this.$nextTick(() => {
        if (!value) {
          this.huanIndex = this.sitIndex
          this.sitIndex = ''
        } else {
          if (this.huanIndex && !this.sitIndex) {
            this.sitIndex = this.huanIndex
          }
        }
      })
    },
    zoom (value) {
      this.$nextTick(() => {
        if (value <= 11) {
          this.zoom = 11
        }
      })
    }
  }
}
</script>

<style scoped>
#map {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  margin: 0;
  font-family: "微软雅黑";
}
.map {
  width: 100%;
  height: 100%;
}
.BMap_cpyCtrl {
  display: none;
}
.anchorBL {
  display: none;
}
</style>
